@isTest
private class NationalAccountEventsTest {

    static ID aasMasterClientRT;
    static ID aasAgencyRT ;
    static ID aasSalesAccountRT ;
    static ID nsMasterClientRT ;
        
    static ID cmrGroupRT ;
    static ID cmrAgencyRT ;
    static ID cmrAgencyClientRT ;
    
    static ID aFrontierManagedCustomer;
    static ID ingenioManagedCustomer ;    
    
    public static void Init(){
        aasMasterClientRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'AAS_Master_Client');
        aasAgencyRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'AAS_Agency_Account');
        aasSalesAccountRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'Advanced_Ad_Solutions');

        nsMasterClientRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'NS_Master_Client');
        cmrGroupRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'CMR_Group');        
        cmrAgencyRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'CMR_Agency');        
        cmrAgencyClientRT = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'CMR_Agency_Client');

        aFrontierManagedCustomer= RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'CMR_Agency_Managed');
        ingenioManagedCustomer = RecordTypeHelper.GetRecordTypeIDByDevName('Account', 'National_Managed_Customer_Ingenio');
    }

    static testMethod void CreateNSMasterClient(){
        Init();
        Account a = PrepareValidNSMasterClient();
                
        Test.startTest();
        
        AssertInsertSuccess(a);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, Master_Client_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Master_Client_ID__c!= null, 'Master Client ID should not be null');
        System.Assert(dbA.Nasa_ID__c == dba.Master_Client_ID__c, 'Master Client ID should match Nasa ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Draft', 'Account Status should be Draft for new accounts'); 
    }
    
    static testMethod void UpdateNSMasterClient(){
        Init();
        Account a = PrepareValidNSMasterClient();
        AssertInsertSuccess(a);
                
        Account testA; 
                
        Test.startTest();
        
        // Invalid Group ID (must be under 9999)
        testa = new Account(ID = a.ID);
        testa.Master_Client_ID__c  = '123456789'; 
        AssertUpdateFails(testa, 'Validation should prevent changing the Master Client ID');
        
        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        AssertUpdateSuccess(testa);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, Master_Client_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Master_Client_ID__c!= null, 'Master Client ID should not be null');
        System.Assert(dbA.Nasa_ID__c == dba.Master_Client_ID__c, 'Master Client ID should match Nasa ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Active', 'Account Status should be Active');        
    }

    static testMethod void CreateAasMasterClient(){
        Init();
        Account a = PrepareValidAasMasterClient();
                
        Test.startTest();
        
        AssertInsertSuccess(a);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, Master_Client_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Master_Client_ID__c!= null, 'Master Client ID should not be null');
        System.Assert(dbA.Nasa_ID__c == dba.Master_Client_ID__c, 'Master Client ID should match Nasa ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Draft', 'Account Status should be Draft for new accounts'); 
    }
    
    static testMethod void UpdateAasMasterClient(){
        Init();
        Account a = PrepareValidAasMasterClient();
        AssertInsertSuccess(a);
                
        Account testA; 
                
        Test.startTest();
        
        // Invalid Group ID (must be under 9999)
        testa = new Account(ID = a.ID);
        testa.Master_Client_ID__c  = '123456789'; 
        AssertUpdateFails(testa, 'Validation should prevent changing the Master Client ID');
        
        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        AssertUpdateSuccess(testa);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, Master_Client_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Master_Client_ID__c!= null, 'Master Client ID should not be null');
        System.Assert(dbA.Nasa_ID__c == dba.Master_Client_ID__c, 'Master Client ID should match Nasa ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Active', 'Account Status should be Active');        
    }
    static testMethod void CreateCmrGroup(){
        Init();
        Account a = PrepareValidCmrGroup();
                
        Test.startTest();
        
        AssertInsertSuccess(a);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, CMR_Group_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.Nasa_ID__c.startswith('G'), 'NASA ID should start with G');
        System.Assert(dbA.Nasa_ID__c == 'G' + dbA.Cmr_Group_ID__c , 'NASA ID should match CMR Group ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Draft', 'Account Status should be Draft for new accounts');        
    }

    static testMethod void UpdateCmrGroup(){
        Init();
        Account a = PrepareValidCmrGroup();
        AssertInsertSuccess(a);
                
        Account testA; 
                
        Test.startTest();
        
        // Invalid Group ID (must be under 9999)
        testa = new Account(ID = a.ID);
        testa .Cmr_Group_ID__c  = '12345'; 
        AssertUpdateFails(testa, 'Expected Group ID validation to fail');
        
        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        testa.Cmr_Group_ID__c  = '9882'; // You should be able to change the ID since the group is a draft
        AssertUpdateSuccess(testa);

        // You should not be able to change the CMR Group ID of and active group
        testa = new Account(ID = a.ID);
        testa.Cmr_Group_ID__c = '9883';
        AssertUpdateFails(testa, 'Expected validation to fail updating Group ID for non-draft account');
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, CMR_Group_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.Nasa_ID__c.startswith('G'), 'NASA ID should start with G');
        System.Assert(dbA.Nasa_ID__c == 'G' + dbA.Cmr_Group_ID__c , 'NASA ID should match CMR Group ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Active', 'Account Status should be Active');        
    }

    static testMethod void CreateCmrAgency(){
        Init();
        Account a = PrepareValidCmrAgency();
                
        Test.startTest();
        
        AssertInsertSuccess(a);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, CMR_ID__c, AAS_Agency_ID__c, Agency_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.AAS_Agency_ID__c == null, 'AAS Agency ID should be null');
        System.Assert(dbA.Nasa_ID__c == dba.CMR_ID__c, 'NASA ID should match CMR ID');
        System.Assert(dbA.Nasa_ID__c == dba.Agency_ID__c, 'NASA ID should match Agency ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Draft', 'Account Status should be Draft for new accounts'); 
    }

    static testMethod void UpdateCmrAgency(){
        Init();
        Account a = PrepareValidCmrAgency();
        AssertInsertSuccess(a);
        
        Account mc = PrepareValidNSMasterClient();
        AssertInsertSuccess(mc);

        Account newGroup = PrepareValidCmrGroup(1);
        newGroup.Account_Status__c = 'Active';
        AssertInsertSuccess(newGroup);

        Account testA; 
        
        Test.startTest();
        
        // Invalid CMR ID (must be under 999999)
        testa = new Account(ID = a.ID);
        testa.Cmr_ID__c  = '1234567'; 
        AssertUpdateFails(testa, 'Expected CMR ID validation to fail');
                            
        // Parent Must be a CMR Group                            
        testa = new Account(ID = a.ID);
        testa.ParentId = mc.ID;
        AssertUpdateFails(testa, 'Excepted Parent ID validation to fail when trying to link Agency to non-CMR Group.');

        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        AssertUpdateFails(testa, 'Expected validtion to fails since the parent Group is not active.');
        
        testa = new Account(ID = a.ParentID);
        testa.Account_Status__c = 'Active';
        AssertUpdateSuccess(testa); // Make the parent Group active
        
        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        testa.Cmr_ID__c  = '9882'; // You should be able to change the ID since the agency is a draft
        AssertUpdateSuccess(testa);

        // You should not be able to change the CMR ID of and active agency
        testa= new Account(ID = a.ID);
        testa.Cmr_ID__c = '9883';
        AssertUpdateFails(testa, 'Excepting validation to fail when trying to change the CMR ID of an active agency');

        // You should not be able to change the CMR ID of and active agency
        testa= new Account(ID = a.ID);
        testa.ParentId = null;
        AssertUpdateFails(testa, 'Excepting validation to fail since an active Agency requires a parent group');

		// Just for fun verify that we can't make the CMR Group inactive now
        testa = new Account(ID = a.ParentID);
        testa.Account_Status__c = 'Inactive';
        AssertUpdateFails(testa, 'Expecting validation to fail when saving the CMR Group. The group is linked to active childern.');

        // You should be able to change the CMR GRoup of an active CMR Agency              
        testa= new Account(ID = a.ID);
        testa.ParentId = newGroup.ID;
        AssertUpdateSuccess(testa);
		
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, CMR_ID__c, AAS_Agency_ID__c, Agency_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.AAS_Agency_ID__c == null, 'AAS Agency ID should be null');
        System.Assert(dbA.Nasa_ID__c == dba.CMR_ID__c, 'NASA ID should match CMR ID');
        System.Assert(dbA.Nasa_ID__c == dba.Agency_ID__c, 'NASA ID should match Agency ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Active', 'Account Status should be Active');        
    }
    static testMethod void CreateAasAgency(){
        Init();
        Account a = PrepareValidAasAgency();
                
        Test.startTest();
        
        AssertInsertSuccess(a);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, CMR_ID__c, AAS_Agency_ID__c, Agency_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.CMR_ID__c == null, 'CMR ID should be null');
        System.Assert(dbA.AAS_Agency_ID__c != null, 'AAS Agency ID should not be null');
        System.Assert(dbA.Nasa_ID__c == dba.AAS_Agency_ID__c, 'NASA ID should match AAS Agency ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.AAS_Agency_ID__c == dba.Agency_ID__c, 'AAS Agency ID should match Agency ID');
        System.Assert(dbA.Account_Status__c == 'Draft', 'Account Status should be Draft for new accounts'); 
    }

    static testMethod void UpdateAasAgency(){
        Init();
        Account a = PrepareValidAasAgency();
        AssertInsertSuccess(a);
        
        Account mc = PrepareValidNSMasterClient();
        AssertInsertSuccess(mc);

        Account testA; 
        
        Test.startTest();
                                            
        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        AssertUpdateSuccess(testa);

        // You should not be able to change the AAS Agency ID of and active agency
        testa= new Account(ID = a.ID);
        testa.AAS_Agency_ID__c = '123123';
        AssertUpdateFails(testa, 'Excepting validation to fail when trying to change the AAS Agency ID of an active agency');
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, CMR_ID__c, AAS_Agency_ID__c, Agency_ID__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.CMR_ID__c == null, 'CMR ID should be null');
        System.Assert(dbA.AAS_Agency_ID__c != null, 'AAS Agency ID should not be null');
        System.Assert(dbA.Nasa_ID__c == dba.AAS_Agency_ID__c, 'NASA ID should match AAS Agency ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.AAS_Agency_ID__c == dba.Agency_ID__c, 'AAS Agency ID should match Agency ID');
        System.Assert(dbA.Account_Status__c == 'Active', 'Account Status should be Active');        
    }

    static testMethod void CreateCmrAgencyClient(){
        Init();
        Account a = PrepareValidCmrAgencyClient();
                
        Test.startTest();
        
        AssertInsertSuccess(a);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, Client_Number__c, CMR_Client_Code_Text__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Client_Number__c != null, 'Client Number should not be null');
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.CMR_Client_Code_Text__c != null, 'CMR ID - Client Code field should not be null');
        System.Assert(dbA.CMR_Client_Code_Text__c == dbA.Nasa_ID__c, 'CMR ID - Client Code field should match the NASA ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Draft', 'Account Status should be Draft for new accounts'); 
    }

    static testMethod void UpdateCmrAgencyClient(){
        Init();
        Account a = PrepareValidCmrAgencyClient();
        AssertInsertSuccess(a);
        
        Account newNSMaster= PrepareValidNSMasterClient();
        newNSMaster.Account_Status__c = 'Active';
        AssertInsertSuccess(newNSMaster);
        
        Account testA; 
        
        Test.startTest();
        
        // Invalid Client Number (must be under 9999)
        testa = new Account(ID = a.ID);
        testa.Client_Number__c = '12345';
        AssertUpdateFails(testa, 'Expected Client Number validation to fail');
                            
        // Invalid Print CMR ID (must be under 9999)
        testa = new Account(ID = a.ID);
        testa.Print_CMR_ID__c = '12345';
        AssertUpdateFails(testa, 'Expected Print CMR ID validation to fail');                            

        // Invalid Print Client Code (must be under 9999)
        testa = new Account(ID = a.ID);
        testa.Print_CMR_ID__c = '12345';
        AssertUpdateFails(testa, 'Expected Print Client Code validation to fail');                            
        
        // CMR 'parent' Must be a CMR Agency
        testa = new Account(ID = a.ID);
        testa.Agency__c = a.Master_Client__c;
        AssertUpdateFails(testa, 'Excepted CMR Agency lookup validation to fail since the record type is incorrect.');

        // Master Client 'parent' Must be an NS Master Client
        testa = new Account(ID = a.ID);
        testa.Master_Client_ID__c = a.Agency__c;
        AssertUpdateFails(testa, 'Excepted NS Master Client lookup validation to fail since the record type is incorrect.');
        
        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        AssertUpdateFails(testa, 'Expect validation to fail trying to activate an account with inactive parents');

		// Activate Agency (and group)
		Account agency = [select id, parentId from account where id = :a.Agency__c];
        testa = new Account(ID = agency.parentId); // Activate the group
        testa.Account_Status__c = 'Active';
        AssertUpdateSuccess(testa);
		
        testa = new Account(ID = a.Agency__c);
        testa.Account_Status__c = 'Active';
        AssertUpdateSuccess(testa);
        
		// Activate Master Client	
        testa = new Account(ID = a.Master_Client__c);
        testa.Account_Status__c = 'Active';
        AssertUpdateSuccess(testa);

        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        testa.Client_Number__c  = '9882'; // You should be able to change the ID since the client is a draft
        AssertUpdateSuccess(testa);

        // You should not be able to change the CMR ID of and active agency
        testa= new Account(ID = a.ID);
        testa.Client_Number__c = '9883';
        AssertUpdateFails(testa, 'Excepting validation to fail when trying to change the Client number of an active CMR Client Account');

		// You should be able to change the Master Client of an active CMR Agency Client
        testa = new Account(ID = a.ID);
        testa.Master_Client__c = newNSMaster.ID;
        AssertUpdateSuccess(testa);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, Client_Number__c, CMR_Client_Code_Text__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Client_Number__c != null, 'Client Number should not be null');
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.CMR_Client_Code_Text__c != null, 'CMR ID - Client Code field should not be null');
        System.Assert(dbA.CMR_Client_Code_Text__c == dbA.Nasa_ID__c, 'CMR ID - Client Code field should match the NASA ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Active', 'Account Status should be Active');        
    }

    static testMethod void CreateAasSalesAccount(){
        Init();
        Account a = PrepareValidAasSalesAccount();
                
        Test.startTest();
        
        AssertInsertSuccess(a);
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, Client_Number__c, CMR_Client_Code_Text__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Client_Number__c != null, 'Client Number should not be null');
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.CMR_Client_Code_Text__c != null, 'CMR ID - Client Code field should not be null');
        System.Assert(dbA.CMR_Client_Code_Text__c == dbA.Nasa_ID__c, 'CMR ID - Client Code field should match the NASA ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Draft', 'Account Status should be Draft for new accounts'); 
    }

    static testMethod void UpdateAasSalesAccount(){
        Init();
        Account a = PrepareValidAasSalesAccount();
        AssertInsertSuccess(a);
        
        Account nsMaster = PrepareValidNSMasterClient();
        AssertInsertSuccess(nsMaster);

        Account testA; 
        
        Test.startTest();
        
        // Valid Client Number for AAS
        testa = new Account(ID = a.ID);
        testa.Client_Number__c = '12345';
        AssertUpdateSuccess(testa);
                            
        // Invalid Print CMR ID (must be under 9999)
        testa = new Account(ID = a.ID);
        testa.Print_CMR_ID__c = '12345';
        AssertUpdateFails(testa, 'Expected Print CMR ID validation to fail');                            

        // Invalid Print Client Code (must be under 9999)
        testa = new Account(ID = a.ID);
        testa.Print_CMR_ID__c = '12345';
        AssertUpdateFails(testa, 'Expected Print Client Code validation to fail');                            
        
        // CMR 'parent' Must be a CMR Agency
        testa = new Account(ID = a.ID);
        testa.AAS_Agency_Name__c = a.Master_Client__c;
        AssertUpdateFails(testa, 'Excepted AAS Agency lookup validation to fail since the record type is incorrect.');

        // Master Client 'parent' Must be an NS Master Client
        testa = new Account(ID = a.ID);
        testa.Master_Client_ID__c = nsMaster.ID;
        AssertUpdateFails(testa, 'Excepted AS Master Client lookup validation to fail since the record type is incorrect.');
        
        // Parents must be active to activate
        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        AssertUpdateFails(testa, 'Excepted validation failure trying to active an account with inactive parents');
	
		// Activate Agency	
        testa = new Account(ID = a.AAS_Agency_Name__c);
        testa.Account_Status__c = 'Active';
        AssertUpdateSuccess(testa);
        
		// Activate Master Client	
        testa = new Account(ID = a.Master_Client__c);
        testa.Account_Status__c = 'Active';
        AssertUpdateSuccess(testa);

        testa = new Account(ID = a.ID);
        testa.Account_Status__c = 'Active';
        testa.Client_Number__c  = '9882'; // You should be able to change the ID since the client is a draft
        AssertUpdateSuccess(testa);

        // You should not be able to change the CMR ID of and active agency
        testa= new Account(ID = a.ID);
        testa.Client_Number__c = '9883';
        AssertUpdateFails(testa, 'Excepting validation to fail when trying to change the Client number of an active AAS Sales Account');
        
        Test.stopTest();
        
        Account dbA = [SELECT ID, Nasa_ID__c, NASA_ID_Calc__c, Client_Number__c, CMR_Client_Code_Text__c, Account_Status__c  from Account where ID = :a.ID];
        System.Assert(dbA.Client_Number__c != null, 'Client Number should not be null');
        System.Assert(dbA.Nasa_ID__c != null, 'NASA ID should not be null');
        System.Assert(dbA.CMR_Client_Code_Text__c != null, 'CMR ID - Client Code field should not be null');
        System.Assert(dbA.CMR_Client_Code_Text__c == dbA.Nasa_ID__c, 'CMR ID - Client Code field should match the NASA ID');
        System.Assert(dbA.Nasa_ID__c == dba.NASA_ID_Calc__c, 'Nasa ID should match Calculated Nasa ID');
        System.Assert(dbA.Account_Status__c == 'Active', 'Account Status should be Active');        
    }
    
    static Account PrepareValidNSMasterClient(){       
        Account a = new Account();
        a.RecordTypeId = nsMasterClientRT ;
        a.Name = 'Test NS Master Client';
        
        return a;
    }
    
    static Account PrepareValidAasMasterClient(){       
        Account a = new Account();
        a.RecordTypeId = aasMasterClientRT ;
        a.Name = 'Test AAS Master Client';
        
        return a;
    }

    static Account PrepareValidCmrGroup(){
    	return PrepareValidCmrGroup(0);
    }       
    static Account PrepareValidCmrGroup(integer x){       
        Account a = new Account();
        a.RecordTypeId = cmrGroupRT ;
        a.Name = 'Test Cmr Group';
        a.Cmr_Group_ID__c = String.ValueOf(9876 + x);
        
        return a;
    }
    
    static Account PrepareValidAasAgency(){       
        Account a = new Account();
        a.RecordTypeId = aasAgencyRT ;
        a.Name = 'Test Aas Agency';
        
        return a;
    }
    
    static Account PrepareValidCmrAgency(){       
        Account a = new Account();
        a.RecordTypeId = cmrAgencyRT ;
        a.Name = 'Test Cmr Agency';
        
        Account cmrGroup = PrepareValidCmrGroup();
        AssertInsertSuccess(cmrGroup);
        a.ParentID = cmrGroup.ID;
        a.CMR_ID__c = '8888';
        return a;
    }

    static Account PrepareValidCmrAgencyClient(){       
        Account a = new Account();
        a.RecordTypeId = cmrAgencyClientRT;
        a.Name = 'Test Cmr Agency Client';
        
        Account cmrAgency = PrepareValidCmrAgency();
        AssertInsertSuccess(cmrAgency);
        a.Agency__c = cmrAgency.ID;
        
        Account masterClient = PrepareValidNSMasterClient();
        AssertInsertSuccess(masterClient);
        a.Master_Client__c = masterClient.ID;
        
        a.Client_Number__c = '3828';
        
        return a;
    }
    
    static Account PrepareValidAasSalesAccount(){       
        Account a = new Account();
        a.RecordTypeId = aasSalesAccountRT;
        a.Name = 'Test AAS Sales Account';
        
        Account aasAgency = PrepareValidAasAgency();
        AssertInsertSuccess(aasAgency);
        a.AAS_Agency_Name__c = aasAgency.ID;
        
        Account masterClient = PrepareValidAasMasterClient();
        AssertInsertSuccess(masterClient);
        a.Master_Client__c = masterClient.ID;
        
        return a;
    }

    static void AssertInsertSuccess(sobject obj){
        insert obj;
    } 
 
    static void AssertUpdateSuccess(sobject obj){
        update obj;
    } 
    
    static void AssertInsertFails(sobject obj, string message){
        try{
            insert obj;
            System.Assert(false, 'Expected insertion to fail. It succeeded. Message:' + message + ' Object:' + obj);
        }
        catch(Exception e){
            System.Debug('Insertion of object failed AS EXPECTED. Exception:' + e);
        }    
    }

    static void AssertUpdateFails(sobject obj, string message){
        try{
            update obj;
            System.Assert(false, 'Expected update to fail. It succeeded. Message:' + message+ ' Object:' + obj);
        }
        catch(Exception e){
            System.Debug('Update of object failed AS EXPECTED. Exception:' + e);
        }    
    }

}